﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StockMatch.Service.Entity;
using Titan;

namespace StockMatch.Service
{
    public class OnlineMessageService:ContextBoundObject
    {


        #region 增删改

        public bool Insert(OnlineMessage onlineMessage)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    return cn.Insert(onlineMessage) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        public bool Update(OnlineMessage onlineMessage, PropertyExpression[] updateProperty)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    return cn.Update(onlineMessage, updateProperty) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex);
            }
        }

        public bool Delete(int onlineMessageId)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    var onlineMessage = new OnlineMessage() { OnlineMessageId = onlineMessageId };
                    return cn.Delete(onlineMessage) > 0;
                    //删除相关的回复数据
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        #endregion


        #region 查询

        public OnlineMessage GetOnlineMessage(int onlineMessageId)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    var onlineMessage = new OnlineMessage() { OnlineMessageId = onlineMessageId };
                    if (cn.Select(onlineMessage))
                    {
                        return onlineMessage;
                    }
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        public OnlineMessages GetOnlineMessagees(QueryExpression query)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    OnlineMessages onlineMessagees = new OnlineMessages();
                    onlineMessagees.TotalCount = cn.SelectCollection(onlineMessagees.Items, query);
                    return onlineMessagees;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        #endregion
    }
}
